package  main.java.leetcode.editor.cn;
//2022-05-01 18:21:15
//给你 root1 和 root2 这两棵二叉搜索树。请你返回一个列表，其中包含 两棵树 中的所有整数并按 升序 排序。. 
//
// 
//
// 示例 1： 
//
// 
//
// 
//输入：root1 = [2,1,4], root2 = [1,0,3]
//输出：[0,1,1,2,3,4]
// 
//
// 示例 2： 
//
// 
//
// 
//输入：root1 = [1,null,8], root2 = [8,1]
//输出：[1,1,8,8]
// 
//
// 
//
// 提示： 
//
// 
// 每棵树的节点数在 [0, 5000] 范围内 
// -105 <= Node.val <= 105 
// 
// Related Topics 树 深度优先搜索 二叉搜索树 二叉树 排序 
// 👍 127 👎 0

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

class AllElementsInTwoBinarySearchTrees {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new AllElementsInTwoBinarySearchTrees().new Solution();
        TreeNode TreeNode1 = new AllElementsInTwoBinarySearchTrees().new TreeNode(2);
        TreeNode TreeNode2 = new AllElementsInTwoBinarySearchTrees().new TreeNode(1);
        TreeNode1.left = new AllElementsInTwoBinarySearchTrees().new TreeNode(1);
        TreeNode1.right = new AllElementsInTwoBinarySearchTrees().new TreeNode(4);
        TreeNode2.left = new AllElementsInTwoBinarySearchTrees().new TreeNode(0);
        TreeNode2.right = new AllElementsInTwoBinarySearchTrees().new TreeNode(3);
        solution.getAllElements(TreeNode1,TreeNode2);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {

        List<Integer> ans = new ArrayList<>();
//        List<Integer> rootlist1 = new ArrayList<>();
//        List<Integer> rootlist2 = new ArrayList<>();
//        //归并
//        dfs(rootlist1,root1);
//        dfs(rootlist2,root2);
//        //排序
//        int n1 = rootlist1.size();
//        int n2 = rootlist2.size();
//        int i = 0;
//        int j = 0;
//        while (i<n1||j<n2){
//
//            if(i == n1){
//                int num2 = rootlist2.get(j);
//                ans.add(num2);
//                j++;
//                continue;
//            }
//            if(j == n2){
//                int num1 = rootlist1.get(i);
//                ans.add(num1);
//                i++;
//                continue;
//            }
//            int num1 = rootlist1.get(i);
//            int num2 = rootlist2.get(j);
//            if(num1<=num2){
//                ans.add(num1);
//                i++;
//            }else {
//                ans.add(num2);
//                j++;
//            }
//        }
        //快排
        dfs(ans,root1);
        dfs(ans,root2);
        Collections.sort(ans);
        return ans;
    }
    public void dfs(List<Integer> list,TreeNode root){
        //中序遍历
        if(root == null){
            return;
        }
        dfs(list,root.left);
        list.add(root.val);
        dfs(list,root.right);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
